package com.hkts.naturalgas.base.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.base.entity.Layout;
import com.hkts.naturalgas.base.entity.Layout;
import com.hkts.naturalgas.base.mapper.LayoutMapper;
import com.hkts.naturalgas.base.service.ILayoutService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.utils.resultMapUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-06-24
 */
@Service
@Transactional
public class LayoutServiceImpl extends ServiceImpl<LayoutMapper, Layout> implements ILayoutService {


    @Autowired
    private LayoutMapper layoutMapper;

    @Override
    public IPage<Layout> selectPage(Page<Layout> page, Layout layout){
            QueryWrapper<Layout> query=Wrappers.query();
            QueryWrapper<Layout> entityWrapper=getEntityWrapper(query, layout);
            return layoutMapper.selectPage(page,entityWrapper);
    }


    @Override
    public List<Layout> selectList(Layout layout){
            QueryWrapper<Layout> query=Wrappers.query();
            QueryWrapper<Layout> entityWrapper=getEntityWrapper(query, layout);
            return layoutMapper.selectList(entityWrapper);
    }

    /**
     *  公共查询条件
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<Layout> getEntityWrapper(QueryWrapper<Layout> entityWrapper,Layout layout){
            //条件拼接
            return entityWrapper;
    }

    @Override
    public IPage<Layout> selectPageByTime(Page<Layout> page, String startDate, String endDate){
        QueryWrapper<Layout> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= '" + startDate + "'")
                .apply(!"".equals(endDate),
                        "time <= '" + endDate + "'")
                .orderByDesc(Layout::getTime);
        IPage<Layout> hktsTailGasPage = layoutMapper.selectPage(page, query);
        return hktsTailGasPage;
    }
    @Override
    public IPage<Layout> getBrokenLineGraph(Page<Layout> page, String startDate, String endDate){
        QueryWrapper<Layout> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= ' " + startDate + " ' " )
                .apply(!"".equals(endDate),
                        "time <= ' " + endDate + " ' ")
                .orderByAsc(Layout::getTime);
        IPage calcinationDesulfurizationDenitrations = layoutMapper.selectPage(page,query);
        List<Layout> records = calcinationDesulfurizationDenitrations.getRecords();
        List<Map<String,Object>> resultDate = records.stream().map(t -> assemblyCargoData(t)).collect(Collectors.toList());
        Map<String, List<Object>> stringListMap = resultMapUtils.resultData(resultDate);
        List<Object> list1 = resultMapUtils.getList(resultDate);
        List<Object> listName = resultMapUtils.getListName(resultDate);
        Map<String,Object> data = new HashMap<>();
        data.put("list",listName);
        data.put("records",stringListMap);
        data.put("times",list1);
        List<Object> resultList = new ArrayList<>();
        resultList.add(data);
        IPage iPage = calcinationDesulfurizationDenitrations.setRecords(resultList);
        return iPage;
    }

    public Map<String,Object> assemblyCargoData(Layout vo){
        Map<String,Object> data = new LinkedHashMap<>();
        data.put("东线排料3号炉",vo.getDxpl3());
        data.put("东线排料4号炉",vo.getDxpl4());
        data.put("东线停排3号炉",vo.getDxtp3());
        data.put("东线停排4号炉",vo.getDxtp4());
        data.put("西线排料3号炉",vo.getXxpl3());
        data.put("西线排料4号炉",vo.getXxpl4());
        data.put("西线停排3号炉",vo.getXxtp3());
        data.put("西线停排4号炉",vo.getXxtp4());
        data.put("time",vo.getTime());
        return data;
    }
}
